﻿/*
 * Author: Branimir Topic (uxxx4663)
 * For: UC - ODD - Assignment 2B
 */
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Web.Security;
using ARMS.BaseClasses;
using System.Data;

namespace ARMS.Models
{
    public class Shop
    {
        [Required]
        [Display(Name = "Shop ID"), MaxLength(100)]
        public int id { get; set; }

        [Display(Name = "Name"), MaxLength(100)]
        public string name { get; set; }

        [Display(Name = "State"), MaxLength(25)]
        public string state { get; set; }

        [Display(Name = "Address"), MaxLength(150)]
        public string address { get; set; }

        [Display(Name = "Code"),MaxLength(10)]
        public string code { get; set; }

        public Shop() { } 
        public Shop(int id, string name, string state, string address, string code)
        {
            this.id = id;
            this.name = name;
            this.state = state;
            this.address = address;
            this.code = code;
        }
    }
    public class ShopModel : BaseModel
    {
        public Shop GetShop(int shopid)
        {
            string select = @"SELECT * FROM shop";
            string where = @"";

            where += String.Format(" id = {0}", shopid);
            string sql = (where.Length > 0) ? select + " WHERE " + where : select;

            Shop shop = new Shop();
            try
            {
                DataTable data = OpenTable(sql);
                DataRow row = data.Rows[0];

                shop.id = Convert.ToInt32(row["id"].ToString());
                shop.name = row["name"].ToString();
                shop.state = row["state"].ToString();
                shop.address = row["address"].ToString();
                shop.code = row["code"].ToString();
            }
            catch (Exception e)
            {
                throw new Exception("Error reading shop record", e);
            }
            return shop;
        }
        public List<Shop> GetShops(int shopid = 0)
        {
            string select = @"SELECT * FROM shop";
            string where = @"";

            if (shopid > 0) where += String.Format(" shopid = {0}", shopid);

            string sql = (where.Length > 0) ? select + " WHERE " + where : select;
            sql += " ORDER BY d, id";

            List<Shop> list = new List<Shop>();
            try
            {
                DataTable data = OpenTable(sql);

                foreach (DataRow row in data.Rows)
                {
                    list.Add(new Shop(
                        Convert.ToInt32(row["id"].ToString()),
                        row["name"].ToString(),
                        row["state"].ToString(),
                        row["address"].ToString(),
                        row["code"].ToString())
                        );
                }
            }
            catch (Exception e)
            {
                throw new Exception("Error reading shop data", e);
            }
            return list;
        }
        public bool SaveShop(int id, string name, string state, string address, string code)
        {
            string update = @"UPDATE shop SET name='{0}', state='{1}', address='{2}', code='{3}'";
            string sql = String.Format(update, name, state, address, code);
            int result = Helper.DummyValue;

            try
            {
                result = Execute(sql);
            }
            catch (Exception e)
            {
                throw new Exception("Error updating shop record", e);
            }

            return result != 0;
        }
        internal int CreateShop(string name, string state, string address, string code)
        {
            string insert = @"INSERT INTO shop (name, state, address, code) VALUES('{0}','{1}','{2}','{3}')";
            string sql = String.Format(insert, name, state, address, code);
            int result;

            try
            {
                result = Execute(sql);
            }
            catch (Exception e)
            {
                throw new Exception("Error creating shop record", e);
            }

            return result;
        }
    }
}
